﻿using System;
using System.Windows;
using DSPL.Services;
using DSPL.Contracts;
using DSPL.Common;
using System.Data;
using DSPL.Services.Services;
using DSPL.Contracts.Common;
using DSPL.Contracts.ViewModel;
using DSPL.Contracts.Command;
using DSPL.Finac.Common;
using System.Windows.Input;

namespace DSPL.Finac.ViewModel
{
    public class ReasonMasterViewModel : BaseViewModel
    {
        private bool isReasonCodeFocused;
        private bool isReasonNameFocused;
        private bool isReasonTypeFocused;
        private string _reasonTypeName;
        private DataTable _dtReasonType=null;
        private ICommand reasonTypeChanged;
        private bool isReasonTypeEnabled;

        #region Define properties
        public bool IsReasonCodeFocused
        {
            get { return isReasonCodeFocused; }
            set
            {
                isReasonCodeFocused = value;
                this.OnPropertyChanged("IsReasonCodeFocused");
            }
        }

        public bool IsReasonNameFocused
        {
            get { return isReasonNameFocused; }
            set
            {
                isReasonNameFocused = value;
                this.OnPropertyChanged("IsReasonNameFocused");

            }
        }

        public bool IsReasonTypeFocused
        {
            get { return isReasonTypeFocused; }
            set
            {
                isReasonTypeFocused = value;
                this.OnPropertyChanged("IsReasonTypeFocused");

            }
        }

        public DataTable DtReasonType
        {
            get { return _dtReasonType; }
            set { _dtReasonType = value;
            this.OnPropertyChanged("DtReasonType");
            }
        }

        public string ReasonTypeName
        {
            get { return _reasonTypeName; }
            set
            {
                _reasonTypeName = value;
                this.OnPropertyChanged("ReasonTypeName");
            }
        }

        public bool IsReasonTypeEnabled
        {
            get
            {
                return isReasonTypeEnabled;
            }

            set
            {
                isReasonTypeEnabled = value;
                this.OnPropertyChanged("IsReasonTypeEnabled");
            }
        }
        #endregion

        #region
        public ICommand ReasonTypeChanged
        {
            get
            {
                if (this.reasonTypeChanged == null)
                {
                    this.reasonTypeChanged = new RelayCommand(ReasonType_SelectionChanged);
                }
                return this.reasonTypeChanged;
            }
        }
        #endregion


        #region Constructor

        public ReasonMasterViewModel()
            : base()
        {
            MessageTitle = "Reason Master";
        }

        #endregion
        /// <summary>
        /// Validate mandatory data before save
        /// </summary>
        /// <param name="obj"></param>
        protected override void PreDataValidateExecuted(object obj)
        {
            IsReasonCodeFocused = false;
            IsReasonNameFocused = false;
            IsReasonTypeFocused = false;
            var messageService = new MessageService();
            if (CurrentRow["ReaCode"].ToString().Trim() == string.Empty)
            {
                messageService.ShowMessage(MessageText.ReasonCodeNotEmpty, MessageTitle, MessageType.InfoOK);
                IsReasonCodeFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["ReaName"].ToString().Trim() == string.Empty)
            {
                messageService.ShowMessage(MessageText.ReasonNameNotEmpty, MessageTitle, MessageType.InfoOK);
                IsReasonNameFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["ReaTypePk"].ToString().Trim() == string.Empty)
            {
                messageService.ShowMessage(MessageText.ReasonTypeNotEmpty, MessageTitle, MessageType.InfoOK);
                IsReasonTypeFocused = true;
                this.IsValid = false;
                return;
            }
            this.IsValid = true;
        }

        protected override void SetDataSourcesAndProperties()
        {
            var getData = new MasterReadService();
            DtReasonType = getData.GetReasonTypes();
            DataColumn[] primaryKeys = new DataColumn[1];
            primaryKeys[0] = DtReasonType.Columns[0];
            DtReasonType.PrimaryKey = primaryKeys;
           

        }

        private void ReasonType_SelectionChanged(object obj)
        {
            string reasonType = CurrentRow["ReaTypePk"].ToString().Trim();
            if (reasonType != string.Empty)
            {
                DataRow selectedRow = _dtReasonType.Rows.Find(reasonType);
                if (selectedRow != null)
                {
                    ReasonTypeName = selectedRow["ReaTname"].ToString().Trim();
                }
            }
            else
            {
                ReasonTypeName = string.Empty;
            }
        }

        protected override void EnableDisableSpecificControls()
        {
            base.EnableDisableSpecificControls();

            if (FormMode == Modes.New)
            {
                IsReasonTypeEnabled = true;
            }
            else
            {
                IsReasonTypeEnabled = false;
            }

        }

        protected override void RefreshExternalDataObjectsExecueted(object obj)
        {

            base.RefreshExternalDataObjectsExecueted(obj);

            if (FormMode == Modes.New)
            {               
                 CurrentRow["Active"] = 1;
                 OnPropertyChanged("CurrentRow");
                
            }

        }

       
    }
}